#include "friendservice.h"
#include "Core/dbcontroler.h"
#include "Core/deallogicobj.h"
#include "Core/mytcpserver.h"
#include "Core/tcpuserconnect.h"
#include "Core/cachemanager.h"
FriendService::FriendService(QObject *parent) : QObject(parent)
{

}

void FriendService::_SenderDoAddFriend(std::shared_ptr<PDU>recvPDU,TcpUserConnect* tcpUser)
{
    (void)tcpUser;

    char * InfoChar=new char[1000000]{'\0'};
    memcpy(InfoChar,(char*)recvPDU.get()+sizeof (PDU),recvPDU->MsgLength);
    QString InfoStr=QString(InfoChar);
    QJsonDocument dc=QJsonDocument::fromJson(InfoStr.toUtf8());
    delete [] InfoChar;
    InfoChar=nullptr;
    if(dc.isNull())return;
    if(!dc.isObject())return;
    QJsonObject InfoJson=dc.object();

    QString SenderName=QString(InfoJson["FROM"].toString());
    QString RecvName=QString(InfoJson["TAG"].toString());
    int status=InfoJson["STATUS"].toInt();
    //发送的请求信息后续完成
    //......

    if(DBControler::get_instance()->_SetFriendRequestInfo(SenderName,RecvName,status)){}

    QJsonObject Info;
    Info["FROM"]=SenderName;
    Info["TAG"]=RecvName;
    Info["STATUS"]=status;

    TcpUserConnect *TO=MyTcpServer::get_instance()->FromUserNameGetTcpSocket(RecvName);
    if(TO!=nullptr)TO->SendMsg(MSG_TYPE_ADD_FRIEND_REQUEST,Info);
    return;
}

void FriendService::_doPullAddFriendRequest(std::shared_ptr<PDU>recvPDU,TcpUserConnect* tcpUser)
{
    char * InfoBody=new char[10000000]{'\0'};
    memcpy(InfoBody,(char*)recvPDU.get()+sizeof(PDU),recvPDU->MsgLength);
    QString InfoJstr=QString(InfoBody);
    delete [] InfoBody;
    InfoBody=nullptr;
    QJsonDocument dc=QJsonDocument::fromJson(InfoJstr.toUtf8());
    if(dc.isNull())return;
    if(!dc.isObject())return;
    QJsonObject InfoJson=dc.object();
    QString Un=InfoJson["MASTER"].toString();



    QList<QMap<QString,QVariant>>AllInfo=DBControler::get_instance()
            ->_PullFriendRequest(Un);

    QJsonObject JsonInfo;
    QJsonArray Inform;
    for(auto & OneInfo:AllInfo){
        QJsonObject Ainf;
        Ainf["SenderId"]=OneInfo["SenderId"].value<QString>();
        Ainf["RequestStatus"]=OneInfo["RequestStatus"].value<int>();
        Ainf["CreateTime"]=OneInfo["CreateTime"].value<QString>();

        Inform.append(Ainf);
    }
    JsonInfo["Inform"]=Inform;

    tcpUser->SendMsg(MSG_TYPE_FRIEND_INFORM_PULL_RESPOND,JsonInfo);

    return;
}

void FriendService::_doSetFriendRespond(std::shared_ptr<PDU>recvPDU,TcpUserConnect* tcpUser)
{
    (void)tcpUser;

    char * InfoChar=new char[1000000]{'\0'};
    memcpy(InfoChar,(char*)recvPDU.get()+sizeof (PDU),recvPDU->MsgLength);
    QString InfoStr=QString(InfoChar);
    QJsonDocument dc=QJsonDocument::fromJson(InfoStr.toUtf8());
    delete [] InfoChar;
    InfoChar=nullptr;
    if(dc.isNull())return;
    if(!dc.isObject())return;
    QJsonObject InfoJson=dc.object();

    QString User1=QString(InfoJson["FROM"].toString());
    QString User2=QString(InfoJson["TAG"].toString());
    int status=InfoJson["STATUS"].toInt();

    QString inf=InfoJson["SAY"].toString();

    DBControler::get_instance()
            ->_MakeFriend(User1,User2,status,inf);
    NoticeTagFriendRequest(User1,User2);
}

void FriendService::_doPullFriendListRespond(std::shared_ptr<PDU>recvPDU, TcpUserConnect *tcpUser)
{
    char * Body=new char[100000]{'\0'};
    memcpy(Body,(char*)recvPDU.get()+sizeof(PDU),recvPDU->MsgLength);
    QJsonDocument dc=QJsonDocument::fromJson(QString(Body).toUtf8());
    delete [] Body;Body=nullptr;
    if(dc.isNull())return;
    if(!dc.isObject())return;
    QJsonObject Jbody=dc.object();
    QString User=Jbody["USER"].toString();
    QJsonArray LIST;
    DBControler::get_instance()->_PullFriendList(User,LIST);

    QJsonObject RetBody;
    RetBody["LIST"]=LIST;
    tcpUser->SendMsg(MSG_TYPE_PULL_FRIEND_LIST_RESPOND,RetBody);

}

void FriendService::NoticeTagFriendRequest(QString SendUser, QString TagUser)
{
    if(MyTcpServer::get_instance()->FromUserNameGetTcpSocket(TagUser)!=nullptr){
        QString SendInfo;
        if(!CacheManager::get_instance()->GetCache(CacheManager::USER)->get(SendUser,SendInfo)){
            //数据库
            DBControler::get_instance()->getUserEtc(SendUser,SendInfo);
        }
        QJsonObject Body=PackBodyInfo(SendUser,SendInfo,TagUser);
        MyTcpServer::get_instance()
                ->FromUserNameGetTcpSocket(TagUser)
                ->SendMsg(MSG_TYPE_ADD_FRIEND_RESPOND,Body);
    }
    if(MyTcpServer::get_instance()->FromUserNameGetTcpSocket(SendUser)!=nullptr){
        QString TagInfo;
        //优先缓存
        if(!CacheManager::get_instance()->GetCache(CacheManager::USER)->get(TagUser,TagInfo)){
            //数据库
            DBControler::get_instance()->getUserEtc(TagUser,TagInfo);
        }
        QJsonObject Body=PackBodyInfo(TagUser,TagInfo,SendUser);
        MyTcpServer::get_instance()
                ->FromUserNameGetTcpSocket(SendUser)
                ->SendMsg(MSG_TYPE_ADD_FRIEND_RESPOND,Body);
    }
}
QJsonObject FriendService::PackBodyInfo(QString FaceUser, QString Info, QString SelfUser)
{
    QJsonObject Body;
    Body["FACE"]=FaceUser;
    Body["STATUS"]=1;
    Body["SELF"]=SelfUser;
    Body["FACEINF"]=Info;
    return Body;
}
